import java.util.Scanner;

public class CinemaSystem {
    private static UserManager userManager = new UserManager();
    private static MovieManager movieManager = new MovieManager();
    private static SessionManager sessionManager = new SessionManager();
    private static TicketManager ticketManager = new TicketManager();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        showLogin();
    }

    // 登录界面
    private static void showLogin() {
        System.out.println("=== 影院管理系统 ===");
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        User user = userManager.login(username, password);
        if (user != null) {
            System.out.println("登录成功！");
            switch (user.getUserType()) {
                case "admin":
                    showAdminMenu(user);
                    break;
                case "manager":
                    showManagerMenu(user);
                    break;
                case "receptionist":
                    showReceptionistMenu(user);
                    break;
                default:
                    System.out.println("未知用户类型");
                    showLogin();
            }
        } else {
            System.out.println("用户名或密码错误");
            showLogin();
        }
    }

    // 管理员菜单
    private static void showAdminMenu(User admin) {
        while (true) {
            System.out.println("\n=== 管理员菜单 ===");
            System.out.println("1. 密码管理");
            System.out.println("2. 用户管理");
            System.out.println("3. 退出登录");
            System.out.print("请选择操作: ");
            int choice = getValidIntegerInput();

            switch (choice) {
                case 1:
                    userManager.manageAdminPassword(admin);
                    break;
                case 2:
                    manageUsers(admin);
                    break;
                case 3:
                    System.out.println("退出成功");
                    showLogin();
                    return;
                default:
                    System.out.println("无效操作");
            }
        }
    }

    // 管理员用户管理
    private static void manageUsers(User admin) {
        System.out.println("\n1. 列出所有用户");
        System.out.println("2. 删除用户");
        System.out.println("3. 查询用户");
        System.out.println("4. 增加用户");
        System.out.println("5. 修改用户信息");
        System.out.print("选择操作: ");
        int choice = getValidIntegerInput();

        switch (choice) {
            case 1:
                userManager.listAllUsers();
                break;
            case 2:
                System.out.print("请输入要删除的用户名: ");
                String username = scanner.nextLine();
                userManager.deleteUser(admin, username);
                break;
            case 3:
                queryUser();
                break;
            case 4:
                System.out.print("请输入用户ID: ");
                String userId = scanner.nextLine();
                System.out.print("请输入用户名: ");
                String newUsername = scanner.nextLine();
                System.out.print("请输入用户类型(manager/receptionist): ");
                String type = scanner.nextLine();
                System.out.print("请输入手机号: ");
                String phone = scanner.nextLine();
                userManager.addUser(admin, userId, newUsername, type, phone);
                break;
            case 5:
                System.out.print("请输入要修改的用户名: ");
                String modifyUsername = scanner.nextLine();
                userManager.modifyUserInfo(admin, modifyUsername);
                break;
            default:
                System.out.println("无效选择");
        }
    }
    private static void queryUser() {
        System.out.println("\n1. 按用户ID查询");
        System.out.println("2. 按用户名查询");
        System.out.println("3. 显示所有用户");
        System.out.print("选择查询方式: ");
        int choice = getValidIntegerInput();

        switch (choice) {
            case 1:
                System.out.print("请输入用户ID: ");
                String id = scanner.nextLine();
                User userById = userManager.findUserById(id);
                if (userById != null) {
                    System.out.println("找到用户: " + userById.getUserId() + "(" + userById.getUserType() + ")");
                } else {
                    System.out.println("未找到用户");
                }
                break;
            case 2:
                // 原逻辑不变
                System.out.print("请输入用户名: ");
                String username = scanner.nextLine();
                User user = userManager.findUserByUsername(username);
                if (user != null) {
                    System.out.println("找到用户: " + user.getUserId() + "(" + user.getUserType() + ")");
                } else {
                    System.out.println("未找到用户");
                }
                break;
            case 3:
                userManager.listAllUsers();
                break;
            default:
                System.out.println("无效选择");
        }
    }

    // 经理菜单
    private static void showManagerMenu(User manager) {
        while (true) {
            System.out.println("\n=== 经理菜单 ===");
            System.out.println("1. 密码管理");
            System.out.println("2. 影片管理");
            System.out.println("3. 排片管理");
            System.out.println("4. 查看销售数据");
            System.out.println("5. 退出登录");
            System.out.print("请选择操作: ");
            int choice = getValidIntegerInput();

            switch (choice) {
                case 1:
                    userManager.manageManagerPassword(manager);
                    break;
                case 2:
                    manageMovies(manager);
                    break;
                case 3:
                    manageSessions(manager);
                    break;
                case 4:
                    sessionManager.viewSalesData();
                    break;
                case 5:
                    System.out.println("退出成功");
                    showLogin();
                    return;
                default:
                    System.out.println("无效操作");
            }
        }
    }

    // 经理影片管理
    private static void manageMovies(User manager) {
        System.out.println("\n1. 列出所有影片");
        System.out.println("2. 添加影片");
        System.out.println("3. 修改影片");
        System.out.println("4. 删除影片");
        System.out.println("5. 查询影片");
        System.out.print("选择操作: ");
        int choice = getValidIntegerInput();

        switch (choice) {
            case 1:
                movieManager.listAllMovies();
                break;
            case 2:
                movieManager.addMovie(manager);
                break;
            case 3:
                System.out.print("请输入要修改的影片名: ");
                String movieName = scanner.nextLine();
                movieManager.modifyMovie(manager, movieName);
                break;
            case 4:
                System.out.print("请输入要删除的影片名: ");
                String deleteMovieName = scanner.nextLine();
                movieManager.deleteMovie(manager, deleteMovieName);
                break;
            case 5:
                System.out.print("请输入要查询的影片名: ");
                String queryMovieName = scanner.nextLine();
                Movie movie = movieManager.findMovieByName(queryMovieName);
                if (movie != null) {
                    System.out.println("找到影片: " + movie.getName() + "(" + movie.getScore() + ")");
                } else {
                    System.out.println("未找到影片");
                }
                break;
            default:
                System.out.println("无效选择");
        }
    }

    // 经理排片管理
    private static void manageSessions(User manager) {
        System.out.println("\n1. 增加场次");
        System.out.println("2. 修改场次");
        System.out.println("3. 删除场次");
        System.out.println("4. 列出场次");
        System.out.println("5. 自动排片");
        System.out.print("选择操作: ");
        int choice = getValidIntegerInput();

        switch (choice) {
            case 1:
                sessionManager.addSession(manager);
                break;
            case 2:
                sessionManager.modifySession(manager);
                break;
            case 3:
                sessionManager.deleteSession(manager);
                break;
            case 4:
                sessionManager.listSessions();
                break;
            case 5:
                sessionManager.autoArrangeSessions(manager);
                break;
            default:
                System.out.println("无效选择");
        }
    }

    // 前台菜单
    private static void showReceptionistMenu(User receptionist) {
        while (true) {
            System.out.println("\n=== 前台菜单 ===");
            System.out.println("1. 列出所有影片");
            System.out.println("2. 列出场次");
            System.out.println("3. 查看指定场次座位");
            System.out.println("4. 售票");
            System.out.println("5. 退票");
            System.out.println("6. 退出登录");
            System.out.print("请选择操作: ");
            int choice = getValidIntegerInput();

            switch (choice) {
                case 1:
                    movieManager.listAllMovies();
                    break;
                case 2:
                    sessionManager.listSessions();
                    break;
                case 3:
                    System.out.print("请输入场次ID: ");
                    int sessionId = getValidIntegerInput();
                    sessionManager.showSessionSeats(sessionId);
                    break;
                case 4:
                    ticketManager.sellTicket(receptionist);
                    break;
                case 5:
                    ticketManager.refundTicket(receptionist);
                    break;
                case 6:
                    System.out.println("退出成功");
                    showLogin();
                    return;
                default:
                    System.out.println("无效操作");
            }
        }
    }

    // 获取有效整数输入
    private static int getValidIntegerInput() {
        while (true) {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的整数");
            }
        }
    }
}